﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Hosting;

namespace LynxWiki.ViewModels
{
    public class AttachmentViewData
    {
        public List<AttachFileInfo> Files { get; set; }
        public List<FileTypeSelection> Selections { get; set; }
        public List<DomainModel.AttachmentVersion> Versions { get; set; }

        public string NamespaceSelected { get; set; }
        public string NamespaceCreatedResult { get; set; }
        public string FileTypeSelected { get; set; }
        public string FileUploadResult { get; set; }
        public string Tags { get; set; }
        public string VersionNote { get; set; }

        public string ListAllowEditUser { get; set; }
        public string ListAllowEditRole { get; set; }
        public string ListDenyReadUser { get; set; }
        public string ListDenyReadRole { get; set; }


        public AttachmentViewData()
        {
            Selections = new List<FileTypeSelection>();
            Files = new List<AttachFileInfo>();
            Versions = new List<DomainModel.AttachmentVersion>();
        }
    }

    public class FileTypeSelection
    {
        public int ID { get; set; }
        public string FileType { get; set; }
    }

    public class AttachFileInfo
    {
        private string imageExt = ".jpeg|.jpg|.gif|.png|.webp|.bmp|.psd|.pspimage|.thm|.tif|.tiff|.yuv|.ai|.eps|.ps|.svg";
        private string docsExt = ".doc|.docx|.txt|.text|.xls|.log|.rtf|.pages|.odt|.msg|.wpd|.pdf|.csv|.dat|.mdb|.accdb|.pps|.ppt|.pptx";
        private string videoExt = ".mpeg|.mpg|.mp4|.h264|.mov|.wmv|.vp8|.vp9|.ogg";

        private string wikiText;
        private string icon;

        public string FileName { get; set; }
        public string DirectoryPath { get; set; }
        public string NameSpace { get; set; }

        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd HH:mm:ss}")]
        public DateTime CreatedOn { get; set; }
        public string Extension { get; set; }
        public Int64 Length { get; set; }

        public bool Updateable { get; set; }
        public bool AuthOK { get; set; }
        public bool CheckedOut { get; set; }
        public string CheckedOutUser { get; set; }

        public string WikiText {
            get
            {
                if (string.IsNullOrEmpty(wikiText)) {
                    if (imageExt.Contains(Extension)) {
                        wikiText = ("[image:" + Path.Combine(HostingEnvironment.ApplicationVirtualPath, DirectoryPath, FileName) + "]").Replace("\\", "/");
                    }
                    else if (docsExt.Contains(Extension)) {
                        wikiText = (@"[attach:""" + FileName + @""":" + Path.Combine(HostingEnvironment.ApplicationVirtualPath, DirectoryPath, FileName) + "]").Replace("\\", "/");
                    }
                    else if (videoExt.Contains(Extension)) {
                        wikiText = ("[video:" + Path.Combine(HostingEnvironment.ApplicationVirtualPath, DirectoryPath, FileName) + "]").Replace("\\", "/");
                    }
                    else {
                        wikiText = (@"[attach:""" + FileName + @""":" + Path.Combine(HostingEnvironment.ApplicationVirtualPath, DirectoryPath, FileName) + "]").Replace("\\", "/");
                    }
                }
                return wikiText;
            }
        }

        //private string imageExt = ".jpeg|.jpg|.gif|.png|.webp|.bmp|.psd|.pspimage|.thm|.tif|.tiff|.yuv|.ai|.eps|.ps|.svg";
        //private string docsExt = ".doc|.docx|.txt|.text|.xls|.log|.rtf|.pages|.odt|.msg|.wpd|.pdf|.csv|.dat|.mdb|.accdb|.pps|.ppt|.pptx";

        public string Icon
        {
            get
            {
                if (string.IsNullOrEmpty(icon)) {
                    switch (Extension) {
                        case ".doc":
                        case ".docx":
                            icon = "Images\\attach\\doc.gif";
                            break;
                        case ".xls":
                            icon = "Images\\attach\\xls.gif";
                            break;
                        case ".txt":
                            icon = "Images\\attach\\txt.gif";
                            break;
                        case ".pps":
                        case ".ppt":
                        case ".pptx":
                            icon = "Images\\attach\\ppt.gif";
                            break;
                        default:
                            icon = "Images\\attach\\attach.gif";
                            break;
                    }

                }
                return icon;
            }
        }

        public AttachFileInfo(FileInfo file, bool updatable, string checkOutUser, string nsName, bool authOK)
        {
            FileName = file.Name;
            NameSpace = nsName;

            int start = HostingEnvironment.ApplicationPhysicalPath.Length;
            DirectoryPath = file.DirectoryName.Substring(start, file.DirectoryName.Length - start).Replace("\\", "/");
            CreatedOn = file.CreationTime;
            Extension = file.Extension;
            Length = file.Length;
            Updateable = updatable;
            CheckedOut = !string.IsNullOrEmpty(checkOutUser);
            CheckedOutUser = checkOutUser;
            AuthOK = authOK;
        }

    }
}